<template>
  <div ref="imageWrapper">
    <div class="modelTitle"><h2>末次方结算模型</h2></div>
    <a-card title="查询条件" class="search">
      <div class="table-page-search-wrapper">
        <a-form-model layout="inline" ref="ruleForm" :model="queryParam" :rules="rules" @keyup.enter.native="onSearch">
          <a-row :gutter="24">
            <a-col :md="6" :sm="8">
              <a-form-model-item label="审计单位" prop="orgName">
                <a-input v-model="queryParam.orgName" readOnly placeholder="请选择" @click.native="openOrganModel" />
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="8" :offset="4">
              <span style="float: right; overflow: hidden" class="table-page-search-submitButtons">
                <a-button type="primary" @click="onSearch" icon="search" class="yellow_search_btn">查询</a-button>
                <a-button @click="searchReset" icon="reload" class="clear_btn">清除条件</a-button>
                <a-button
                  type="danger"
                  :loading="ydLoading"
                  @click="handleToYD1()"
                  icon="arrow-right"
                  style="margin-left: 18px"
                  >转疑点</a-button
                >
              </span>
            </a-col>
          </a-row>
        </a-form-model>
      </div>
    </a-card>
    <a-card style="margin-top: 20px">
      <a-table
        ref="table"
        size="middle"
        rowKey="id"
        bordered
        :columns="columns1"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        @change="handleTableChange"
        :row-selection="rowSelection"
        class="antableClass"
        :scroll="{ x: 2000 }"
      >
        <template v-for="item in columns1" v-slot:[item.dataIndex]="text, record">
          <j-ellipsis
            :style="{ color: record.type == '1' ? 'red' : '' }"
            :key="item.dataIndex"
            v-if="
              item.dataIndex != 'ifDelyRc' &&
                item.dataIndex != 'action' &&
                item.dataIndex != 'conName' &&
                item.dataIndex != 'ifAdtnCon'
            "
            :value="text.toString()"
            :length="10"
          />
          <j-ellipsis
            :style="{ color: '#3154a4', cursor: 'pointer' }"
            :key="item.dataIndex"
            v-else-if="item.dataIndex == 'conName'"
            :value="text.toString()"
            :length="10"
            @click.native="CheckItem(record, item.dataIndex)"
          />
          <a
            :key="item.dataIndex"
            v-else-if="item.dataIndex != 'action'"
            href="javaScript:;"
            @click="CheckItem(record, item.dataIndex)"
          >
            {{ text }}
          </a>
          <span :key="item.dataIndex" v-else>
            <a @click="CheckDetail(record)">详情</a>
          </span>
        </template>
        <!-- <span slot="merchName" slot-scope="text, record">
          <a @click="handleChangeCheckList(record)">{{ text }}</a>
        </span> -->
      </a-table>
    </a-card>
    <YDModel ref="YDModel" :routerData="$route.path" />
    <CheckDetail ref="CheckDetail" />
    <AuditDepart ref="AuditDepart" @selectedRow="updateQueryParam" />
  </div>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { columns } from './columns'
import { base64toImg } from '@/mixins/base64toImg'
import YDModel from './YDModel'
import CheckDetail from './CheckDetail.vue'
import TbaleCom from '@/views/sj/sjmxgl/gcxmgl/sbwzgys/TbaleCom'
import { postAction } from '@/api/manage'
import AuditDepart from '@/views/sj/sjmxgl/cwzb/components/AuditDepart'
export default {
  mixins: [JeecgListMixin, base64toImg],
  components: { AuditDepart, YDModel, CheckDetail, TbaleCom },
  computed: {
    // 行选择
    rowSelection() {
      return {
        selectedRowKeys: this.selectedRowKeys,
        onChange: (selectedRowKeys, selectedRows) => {
          this.selectedRowKeys = [...selectedRowKeys]
          this.selectedRows = [...selectedRows]
        }
      }
    }
  },
  watch: {
    $route: {
      handler: function(to) {
        if (Object.keys(to.query).length) {
          debugger
          this.disableMixinCreated = true
          let { orgName, selectedKeys, orgId } = to.query
          this.selectedRowKeys = selectedKeys.split(',')
          this.queryParam = Object.assign(this.queryParam, {
            orgName,
            orgId
          })
          this.$nextTick(() => {
            this.onSearch()
          })
        }
      },
      immediate: true
    }
  },
  data() {
    return {
      disableMixinCreated: true,
      columns1: columns(this),
      queryParam: {
        orgName: '',
        orgId: ''
      },
      superParam: {
        id: 'e64079ce56a16f8c4cfc05519b67sd19',
        modelId: 'e64079ce56a16f8c4cfc05519b67sd19',
        extraFlag: true
      },
      upLoading: false,
      dataSource: [],
      url: {
        list: '/sjmx/queryEavLagData',
        // 查询模型转疑点信息(多选)
        TurnSuspects: '/sjmx/queryModelMsgMulti'
      },
      layout: {
        labelCol: { span: 6 },
        wrapperCol: { span: 18 }
      },
      rules: {
        orgName: [{ required: true, message: '请选择审计单位' }]
      },
      selectedRows: [],
      selectedRowKeys: []
    }
  },
  methods: {
    searchReset() {
      this.dataSource = []
      this.selectedRows = []
      this.selectedRowKeys = []
      this.queryParam = this.$options.data().queryParam
    },
    openOrganModel() {
      this.$refs.AuditDepart.show()
    },
    CheckDetail(record) {
      this.$refs.CheckDetail.edit(record, this.queryParam)
    },
    CheckItem(record, itemKey) {
      this.$refs.CheckDetail.showItemDetail(record, itemKey)
    },
    updateQueryParam(checkRow) {
      console.log(checkRow)
      this.queryParam.orgName = checkRow.departName
      this.queryParam.orgId = checkRow.id
    },
    onSearch() {
      this.$refs.ruleForm.validate(value => {
        if (value) {
          this.loadData(1)
        } else {
          this.$message.warning('请选择审计单位和审计时间')
        }
      })
    },
    handleToYD1(record) {
      if (this.dataSource.length) {
        const { startTime = '', endTime = '' } = this.queryParam
        let ydnr = undefined
        let gzzd = undefined

        let time = `【${startTime}～${endTime}】【末次方结算滞后模型】`
        console.log('this.activeKey------>', this.activeKey)
        let selectedRowKeys = this.selectedRows.map(item => item.id)

        let param = Object.assign({}, this.queryParam, { selectedKeys: selectedRowKeys.join(',') })
        let ydList = this.getselectRowJosn(this.selectedRows)
        console.log(ydList)
        postAction(this.url.TurnSuspects, Object.assign({}, this.queryParam, this.superParam, { ydList: ydList })).then(
          res => {
            if (res.success) {
              if (res.result.length) {
                ydnr = res.result[0].ydms ? res.result[0].ydms : ydnr
                gzzd = res.result[0].gzzd ? res.result[0].gzzd : gzzd
              }
              this.addYdMulti(param, ydnr, time, gzzd)
            }
            if (res.code === 510) {
              this.$message.warning(res.message)
            }
          }
        )
      } else {
        this.$message.error('空数据不能新建疑点')
      }
    },
    getselectRowJosn(selectedRows) {
      selectedRows.map(item => {
        item.zb = item.target
      })
      return JSON.stringify(selectedRows)
    }
  }
}
</script>

<style lang="less" scoped>
@import '~@assets/less/tableTheme.less';
.modelTitle {
  display: flex;
  justify-content: center;
  align-items: center;
  h2 {
    font-weight: 600;
  }
}
</style>
